package zth;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBUtils {
    private static String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static String DB_URL = "jdbc:mysql://localhost:3306/zthjava";
    private static String DB_USER = "root";
    private static String DB_PASS = "zhang1120";

    public static void insert(String sql) throws SQLException {
        execute(sql);
    }

    public static void delete(String sql) throws SQLException {
        execute(sql);
    }

    public static void update(String sql) throws SQLException {
        execute(sql);
    }

    private static void execute(String sql) throws SQLException {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
            try (Statement statement = connection.createStatement()) {
                statement.execute(sql);
            }
        }
    }
    public static Information getInformationByKey(String key) throws SQLException {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Information information = new Information();
        String sql = "SELECT * FROM `information` WHERE" +
                " account =" + key;
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
            try (Statement statement = connection.createStatement()) {
                ResultSet rs = statement.executeQuery(sql);
                if (rs.next()) {
                    information.setaccount(rs.getString("account"));
                    information.setpassword(rs.getString("password"));
                    information.setsex(rs.getString("sex"));
                    information.settype(rs.getString("type"));
                }
            }
        }
        return information;
    }
    public static String GetPassword(String account) throws SQLException {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String sql = "select * from information where account='" + account + "'";
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
            try (Statement statement = connection.createStatement()) {
                ResultSet rs = statement.executeQuery(sql);
                String password = null;
                if (rs.next())
                    password = rs.getString("password");
                    System.out.println(password);
                return password;
            }
        }
    }

    public static String GetType(String account) throws SQLException {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String sql = "select * from information where account='" + account + "'";
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
            try (Statement statement = connection.createStatement()) {
                ResultSet rs = statement.executeQuery(sql);
                String type = null;
                if (rs.next())
                    type = rs.getString("type");
                System.out.println(type);
                return type;
            }
        }
    }
    public static String Getnumber(String account) throws SQLException {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String sql = "select * from information where account='" + account + "'";
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
            try (Statement statement = connection.createStatement()) {
                ResultSet rs = statement.executeQuery(sql);
                String number = null;
                if (rs.next())
                    number = rs.getString( "number");
                return number;
            }
        }
    }

    public static List<Information> getInformation(String sql) throws SQLException {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        List<Information> informations = new ArrayList<Information>();

        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
            try (Statement statement = connection.createStatement()) {
                ResultSet rs = statement.executeQuery(sql);
                while (rs.next()) {
                    Information information = new Information();
                    information.setaccount(rs.getString("account"));
                    information.setpassword(rs.getString("password"));
                    information.setsex(rs.getString("sex"));
                    information.settype(rs.getString("type"));
                    informations.add(information);
                }
            }
        }
        return informations;
    }
    public static List<photoinf> getphotoinf(String sql) throws SQLException {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        List<photoinf> photoinfs = new ArrayList<photoinf>();

        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
            try (Statement statement = connection.createStatement()) {
                ResultSet rs = statement.executeQuery(sql);
                while (rs.next()) {
                    photoinf photoinfz = new photoinf();
                    photoinfz.setName(rs.getString("name"));
                    photoinfz.setTel(rs.getString("tel"));
                    photoinfz.setsex(rs.getString("sex"));
                    photoinfz.setQq(rs.getString("qq"));
                    photoinfs.add(photoinfz);
                }
            }
        }
        return photoinfs;
    }
}

